package com.greenzebras.geocache;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.greenzebras.geocache.cachedata.Cache;
import com.greenzebras.geocache.cachedata.CacheType;
import com.greenzebras.geocache.constant.GeocacheConstants;
import com.greenzebras.geocache.datastore.GeocacheCacheManagementRequestResponseListener;
import com.greenzebras.geocache.datastore.persistence.GeocacheDataService;

public class GeocacheEditActivity extends Activity implements
		GeocacheCacheManagementRequestResponseListener {

	private Cache cache = null;
	private EditText nameEditText = null;
	private EditText descriptionEditText = null;
	private Spinner cacheTypeSpinner = null;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		if (getIntent().hasExtra(GeocacheConstants.CACHE_TO_EDIT)) {
			cache = (Cache) getIntent().getExtras().get(GeocacheConstants.CACHE_TO_EDIT);
		}

		if (null == cache) {
			Toast.makeText(getApplicationContext(), "No cache to edit.", 5)
					.show();
			finishEdit(RESULT_CANCELED);
		} else {
			setContentView(R.layout.edit);
			((TextView) findViewById(R.id.activity_title))
					.setText(R.string.edit);
			// cache form fields
			nameEditText = (EditText) findViewById(R.id.cache_name);
			descriptionEditText = (EditText) findViewById(R.id.cache_description);
			cacheTypeSpinner = ((Spinner) findViewById(R.id.cache_type_spinner));

			populateCacheTypeSpinner();
			populateCacheFields();
			initializeAddressDetails();
			Button commitButton = ((Button) findViewById(R.id.commit_button));
			commitButton.setText(R.string.update_button);
			commitButton.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Cache cacheToUpdate = getCacheToUpdate();
					if (null != cacheToUpdate) {
						GeocacheDataService.INSTANCE.updateCache(
								GeocacheEditActivity.this, cacheToUpdate);
					}
				}
			});
		}
	}

	private Cache getCacheToUpdate() {
		String nameEditTextContents = nameEditText.getText().toString();
		if (nameEditTextContents.isEmpty()) {
			Toast.makeText(getApplicationContext(),
					"Cache Name Cannot Be Empty", 5).show();
			return null;
		} else {
			cache.setName(nameEditTextContents);
			cache.setDescription(descriptionEditText.getText().toString());
			cache.setCacheType((CacheType) cacheTypeSpinner.getSelectedItem());

			return cache;
		}
	}

	private void populateCacheFields() {
		nameEditText.setText(cache.getName());
		descriptionEditText.setText(cache.getDescription());
	}

	private void populateCacheTypeSpinner() {
		ArrayAdapter<CacheType> cacheTypeArrayAdapter = new ArrayAdapter<CacheType>(
				this, android.R.layout.simple_spinner_item, CacheType.values());
		cacheTypeArrayAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		cacheTypeSpinner = ((Spinner) findViewById(R.id.cache_type_spinner));
		cacheTypeSpinner.setAdapter(cacheTypeArrayAdapter);
		cacheTypeSpinner.setSelection(cacheTypeArrayAdapter.getPosition(cache
				.getCacheType()));
	}

	@Override
	public void processGeocacheCacheManagementRequestResponse(
			RequestStatus requestStatus, Cache cache) {
		if (requestStatus.equals(RequestStatus.SUCCESS)) {
			this.cache = cache;
			Toast.makeText(getApplicationContext(),
					getString(R.string.update_success_message), 5).show();
			finishEdit(RESULT_OK);
		} else {
			Toast.makeText(getApplicationContext(),
					getString(R.string.update_error_message), 5).show();
		}
	}

	private void finishEdit(int result) {
		
		Intent resultIntent = new Intent();
		resultIntent.putExtra(GeocacheConstants.CACHE_TO_EDIT, cache);
		
		setResult(result, resultIntent);
		finish();
	}

	private void initializeAddressDetails() {
		GeoPoint cacheGeoPoint = cache.getCacheCoordinate().getGeoPoint();
		if (null != cacheGeoPoint) {
			TextView latitudeTextView = (TextView) findViewById(R.id.cache_latitude);
			latitudeTextView.setText(Double.toString(cacheGeoPoint
					.getLatitudeE6()
					/ GeocacheConstants.LOCATION_COORD_TO_GEOPOINT_COORD_MULTIPLIER));
			TextView longitudeTextView = (TextView) findViewById(R.id.cache_longitude);
			longitudeTextView.setText(Double.toString(cacheGeoPoint
					.getLongitudeE6()
					/ GeocacheConstants.LOCATION_COORD_TO_GEOPOINT_COORD_MULTIPLIER));
		}
	}
}